/*
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <QApplication>
#include "garmin.h"
#include "config.h"
#include "garmin-view.h"
#include "xmlconfigreader.h"
#include "garmindumpreader.h"
#include "runrecord.h"
#include "runrecordset.h"
#include "plotview.h"



int main(int argc, char *argv[]) {
  QApplication app(argc, argv);
  const char *garmin_dir;
  
  XMLConfigReader reader("sample-config.xml");

  reader.createDOM();
  garmin_dir = reader.getConfigValue("GARMIN_SAVE_RUNS",
                                            "0.0.2").toStdString().c_str();
  
  setenv("GARMIN_SAVE_RUNS", garmin_dir, 1);

  //PlotView v;
  if (argc > 1)
  {
  int c;
    while ((c = getopt (argc, argv, "g:k:d:l")) != -1)
    {
      switch (c)
        {
        case 'k':
          // to KML file
          {
            RunRecord r(optarg);
            qDebug(r.toKMLPath().toStdString().c_str());
            return 0;
          }
          break;
        case 'd':
          // describe
          {
            RunRecord r(optarg);
            qDebug(r.toString().toStdString().c_str());
            return 0;
          }
          break;
        case 'l':
          // list files
          {
            RunRecordSet s(garmin_dir);
            printf("loaded %d records\n", s.size());
            int size = s.size();
            int i;

            for (i = 0; i < size; i++)
            {
              RunRecord r = s.getRecord(i);
              printf("%d) %s\n", i, r.getDate().toString().toStdString().c_str());
            }

            printf(">>");
            scanf("%d", &i);
            RunRecord r = s.getRecord(i);
            printf("%s\n", r.toKMLPath().toStdString().c_str());
            return 0;
          }
          break;
        case 'g':
          // graph
          {
            RunRecord r(optarg);
            Track t = r.getTrack();

            Waypoint w;
            int i;
            int size = t.size();
            
            QList<QPointF> l;

            for (i = 0; i < size; i++)
            {
//               // basing waypoint validity on position is bad.
//               if(t.getWaypoint(i).getPosition().isValid())
//               {
//                 l.append(QPointF(i, t.getWaypoint(i).getAlt()));
//               }
//               else
//               {
//                 l.append(QPointF(1.0e21, 0));
//               }
 
              if(t.getWaypoint(i).getPosition().isValid() &&
                  t.getWaypoint(i - 1).getPosition().isValid())
              {
                Waypoint w1 = t.getWaypoint(i - 1);
                Waypoint w2 = t.getWaypoint(i);
                
                double distance = w2.getDistance() - w1.getDistance();
                double time     = w1.getTime().secsTo(w2.getTime());

                double speed = distance / time;

                printf("%f\n", speed);

                if(!l.isEmpty())
                  l.append(QPointF(i, (speed + l.last().y() ) / 2));
                else
                  l.append(QPointF(i, speed));
              }
              else
              {
                l.append(QPointF(1.0e21, 0));
              }
            }
            //v.setData(l);
            //v.setVisible(true);
          }
          break;
        case '?':
          // help
          if (optopt == 'c')
            fprintf (stderr, "Option -%c requires a filename argument.\n", optopt);
          else if (isprint (optopt))
            fprintf (stderr, "Unknown option `-%c'.\n", optopt);
          else
            fprintf (stderr,
                    "Unknown option character `\\x%x'.\n",
                    optopt);
          return 1;
        default:
          abort ();
        }
    }
  }
  else
  {
    return 0;
  }



  return app.exec();
}
